package Io;

import java.io.*;

public class Test7 {
    //缓冲字节流
    public static void main(String[] args) {
//        try {
////            Test7.testBufferedInputstream();
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//        try {
//            Test7.testBufferedoputstream();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        Test7.copyFile("D:\\aa\\a\\tt1.txt","D:\\aa\\a\\tt2.txt");
    }

    public static void testBufferedInputstream() throws Exception {
        //先建文件字节流，再建缓冲字节流
        FileInputStream in = new FileInputStream("D:\\aa\\a\\tt1.txt");
        BufferedInputStream br = new BufferedInputStream(in);
        byte[] b = new byte[10];
        int len = 0;
        while ((len = br.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        br.close();
        in.close();
    }

    public static void testBufferedoputstream() throws Exception {
        //先建文件字节流，再建缓冲字节流
        FileOutputStream out = new FileOutputStream("D:\\aa\\a\\tt2.txt");
        BufferedOutputStream bo = new BufferedOutputStream(out);
        String str = "abcdefg";
        bo.write(str.getBytes());
        bo.flush();
        bo.close();
        out.close();
    }
    public static void copyFile(String inpath,String outpath) {
        try {
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(inpath));
            BufferedOutputStream bo =new BufferedOutputStream(new FileOutputStream(outpath));
            byte[] b =new byte[1024];
            int len = 0;
            while((len=br.read(b))!=-1){
                bo.write(b,0,len);
            }
            bo.flush();
            bo.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
